"""
chn IOS渠道
new_device 新增设备
new_register 新增注册
active_device 活跃设备
active_user 活跃用户
register_rate 当天注册率
device_stay_2_rate 设备次留率
user_stay_2_rate 用户次留率
recharge 充值
recharge_rate 付费率
real_profit 营收
real_profit_rate 营收率
tax 税收
new_recharge_rate 新用户付费率
new_recharge 新增充值金额
updated_up 报表更新时间
"""
S2S_REPORT_CONFIG = {
    'real_time_report': True,
    'group_by': '_s2s_appid.keyword',
    'target_event_id': ['recharge', 'withdraw', 'tax', 'active', 'register', 'bind_phone', 'app_download'],
    'values': [
        {
            "value_id": "click_distinct_count",
            "target_value": "_ip.keyword",
            "value_name": "链接点击数",
            "filter": [
                {'terms': {'_event_id': ['app_download']}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_device",
            "target_value": "_aid.keyword",
            "value_name": "新增设备",
            "filter": [
                {'terms': {'_event_id': ['register']}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_register_device",
            "target_value": "_aid.keyword",
            "value_name": "首日注册设备",
            "filter": [
                {'term': {'_event_id': 'bind_phone'}},
                {'term': {'_is_new_device': 1}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_user",
            "target_value": "_user_id",
            "value_name": "新增用户",
            "filter": [
                {'terms': {'_event_id': ['register']}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_register_user",
            "target_value": "_user_id",
            "value_name": "新增注册用户",
            "filter": [
                {'terms': {'_event_id': ['bind_phone']}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "active_device",
            "target_value": "_aid.keyword",
            "value_name": "活跃设备",
            "filter": [
                {'terms': {'_event_id': ['active']}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "active_user",
            "target_value": "_user_id",
            "value_name": "活跃用户",
            "filter": [
                {'terms': {'_event_id': ['active']}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_rate",
            "value_name": "当天注册率",
            "value_expr": "new_register_user/new_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "recharge_amount",
            "target_value": "recharge_price",
            "value_name": "充值金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
        },
        {
            "value_id": "recharge_user",
            "target_value": "_user_id",
            "value_name": "充值账号",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "recharge_rate",
            "value_name": "付费率",
            "value_expr": "recharge_user/active_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "withdraw_amount",
            "target_value": "withdraw_price",
            "value_name": "提现金额",
            "filter": [
                {'term': {'_event_id': 'withdraw'}},
            ],
        },
        {
            "value_id": "real_profit",
            "target_value": ["recharge_price", "-", "withdraw_price"],
            "value_name": "营收",
            "filter": [
                {'terms': {'_event_id': ['recharge', 'withdraw']}},
            ],
        },
        {
            "value_id": "real_profit_rate",
            "value_name": "营收率",
            "value_expr": "real_profit/recharge_amount",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "tax",
            "target_value": "tax_tax",
            "value_name": "税收",
            "filter": [
                {'term': {'_event_id': 'tax'}},
            ]
        },
        {
            "value_id": "new_recharge_amount",
            "target_value": "recharge_price",
            "value_name": "新增充值金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'term': {'_is_new_user': 1}}
            ],
        },
        {
            "value_id": "new_recharge_user",
            "target_value": "_user_id",
            "value_name": "新增充值账号",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'term': {'_is_new_user': 1}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_recharge_rate",
            "value_name": "新用户付费率",
            "value_expr": "new_recharge_user/new_register_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "next_day_device",
            "target_value": "_aid.keyword",
            "value_name": "设备次留数",
            "filter": [
                {"term": {"_event_id": "active"}},
                {"term": {"_active_days": 1}}
            ],
            "time_range": {
                "_event_time": [1, 1]
            },
            "operation": "cardinality",
        },
        {
            "value_id": "next_day_user",
            "target_value": "_user_id",
            "value_name": "用户次留数",
            "filter": [
                {"term": {"_event_id": "active"}},
                {"term": {"_active_days": 1}}
            ],
            "time_range": {
                "_event_time": [1, 1]
            },
            "operation": "cardinality",
        },
    ]
}


S2S_CHN_BALANCE_CONFIG = {
    'real_time_report': True,
    'target_event_id': ['recharge', 'withdraw', 'tax', 'register', 'bind_phone'],
    'values': [
        {
            "value_id": "active_count",
            "target_value": "_user_id",
            "value_name": "新增活跃用户",
            "filter": [
                {'terms': {'_event_id': ['register']}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_count",
            "target_value": "_user_id",
            "value_name": "新增绑定用户",
            "filter": [
                {'terms': {'_event_id': ['bind_phone']}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "total_recharge",
            "target_value": "recharge_price",
            "value_name": "充值金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ]
        },
        {
            "value_id": "total_withdraw",
            "target_value": "withdraw_price",
            "value_name": "提现金额",
            "filter": [
                {'term': {'_event_id': 'withdraw'}},
            ]
        },
        {
            "value_id": "recharge_count",
            "target_value": "_user_id",
            "value_name": "充值次数",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
            "operation": "value_count"
        },
        {
            "value_id": "withdraw_count",
            "target_value": "_user_id",
            "value_name": "提现次数",
            "filter": [
                {'term': {'_event_id': 'withdraw'}},
            ],
            "operation": "value_count"
        },
        {
            "value_id": "agency_recharge",
            "target_value": "recharge_price",
            "value_name": "代理充值金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'terms': {'recharge_sub_type.keyword': ['agency_pay', 'mf_pay']}},
            ]
        },
        {
            "value_id": "tax_buckets",
            "target_value": "tax_tax",
            "value_name": "税费",
            "terms": {
                "field": "tax_game_id.keyword"
            }
        }
    ]
}


